<?php
/**
 * Geocontexter
 * @link http://code.google.com/p/geocontexter/
 * @package Geocontexter
 */

/**
 * Admin index controller
 *
 *
 * @package Geocontexter
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev$ / $LastChangedDate$ / $LastChangedBy$
 */

namespace Gospecies\Controller;

use Zend\View\Model\ViewModel;
use Gospecies\Controller\AbstractController;


class ContactController extends AbstractController
{

    protected function init($controller)
    {
        $this->initView( 'gospecies/contact/index.phtml');

        $this->view->active_page = 'contact';
        $this->view->error = array();
    }

    public function indexAction()
    {
		$this->view->sent = false;
        $this->view->loop         = rand(0, 765645432);
        $this->view->loop_count   = 30;
        $this->view->loop_display = $this->view->loop + rand(1, $this->view->loop_count);

        $this->sessionOffsetSet('loop_display', $this->view->loop_display);

        return $this->view;
    }

    /**
     * perform on the login request
     *
     */
    public function dosAction()
    {
        $loop_display = '_' . $this->sessionOffsetGet('loop_display');

        $_error = array();

        $submit = $this->request->getPost()->$loop_display;

        if (($submit !== null) && !empty($submit)) {

        	$email   = $this->request->getPost()->contact_email;
        	$content = $this->request->getPost()->contact_content;

        	if (($email === null) || empty($email)) {
        		$_error[] = 'Email field is empty!';
        	}

        	if (($content === null) || empty($content)) {
        		$_error[] = 'Contact field is empty!';
        	}

        	if (count($_error) > 0) {

        		$this->view->sent            = false;
        		$this->view->contact_email   = $email;
        		$this->view->contact_content = $content;
        		$this->view->error           = $_error;

        	} else {
        		$this->view->sent = true;
        	}

        }

        $this->sessionOffsetUnset('loop_display');

        return $this->indexAction();
    }
}
